using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class CategoryList : AdminBaseUserControl
    {

        public event EventHandler TreeNodeClick;
        protected void Page_Load(object sender, EventArgs e)
        {
            string target = Request.Form["__EVENTTARGET"];
            if (!string.IsNullOrEmpty(target))
            {
                if (target.Equals(this.tvCategory.UniqueID))
                {
                    if (ViewState["previousArg"] != null)
                    {
                        string previousArg = (string)ViewState["previousArg"];
                        if (previousArg.Equals(Request.Form["__EVENTARGUMENT"]))
                        {
                            if (TreeNodeClick != null && tvCategory.SelectedNode.Depth == 1)
                            {
                                this.SelectedNode = tvCategory.SelectedValue;

                                TreeNodeClick(this, e);
                            }
                        }
                    }
                    ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
                }
            }

        }

        public string SelectedNode
        {
            get { return Convert.ToString(ViewState["_CategoryID"]); }
            set { ViewState["_CategoryID"] = value; }
        }

        public IList<ICmsClass> DataSource
        {
            set
            {
                ViewState["_categoryist"] = value;
            }
        }

        private void PopulateBind(IContentCategory category, TreeNode node)
        {
            if (category.CategoryList != null)
                foreach (IContentCategory cat in category.CategoryList)
                {
                    TreeNode subNode = new TreeNode(cat.Name, cat.ID.ToString());
                    node.ChildNodes.Add(subNode);
                    PopulateBind(cat, subNode);
                }

        }

        public override void DataBind()
        {
            tvCategory.Nodes.Clear();
            //IContentCategory root = CategoryController.GetCategoryTree(); 
            IContentCategory root = (IContentCategory)ViewState["_categoryist"];
            TreeNode nodeRoot = new TreeNode("Categories", "0");
            nodeRoot.NavigateUrl = "javascript:void(0)";
            PopulateBind(root, nodeRoot);
            tvCategory.Nodes.Add(nodeRoot);
            tvCategory.ExpandAll();
        }

        protected void tvCategory_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (TreeNodeClick != null)
            {
                this.SelectedNode = tvCategory.SelectedValue;

                TreeNodeClick(this, e);
            }
            ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
        }

        public void DoDeleteCategory(object sender, EventArgs e)
        {
            CategoryController.DeleteCategory(Convert.ToInt64(this.SelectedNode));
            DataBind(); 
        }


    }
}